const bodyparser = require("body-parser");
const Mock = require("mockjs");
const data = Mock.mock({
  "tab|4": [
    {
      "title|+1": ["全部", "杭州发布", "杭州日报", "直播"],
      "children|20": [
        {
          title: "@ctitle",
          image: "@image(120x80, @color)",
          date: "@date",
        },
      ],
    },
  ],
  "list|10": [
    {
      title: "@ctitle",
      image: "@image(120x80, @color)",
      date: "@date",
      id: "@id",
    },
  ],
});

module.exports = (app) => {
  // 必须使用该中间件， 不用的情况下， req.body => undefined
  app.use(bodyparser.json());

  app.post("/api/login", (req, res) => {
    const { username, password } = req.body;
    if (username === "admin" && password === "admin") {
      res.send({
        code: 200,
        message: "登陆成功， 欢迎 admin 用户",
      });
    } else {
      res.send({
        code: 400,
        message: "账户密码错误， 请重新输入",
      });
    }
  });

  app.get("/api/tab", (req, res) => {
    res.send({
      code: 200,
      data: data.tab,
    });
  });

  app.get('/api/list', (req, res) => {
    const { wd = "" } = req.query;

    const result = data.list.filter(v => {
      return v.title.includes(wd)
    })

    res.send({
      code: 200,
      data: result
    })
  })
};
